var app = angular.module('NTApp').controller('DevicesListCtrl', function($state, $interval, $rootScope, WXService, DeviceService) {
    
      WXService.getCookieData('hvacUserInfo');
      WXService.getOnlineToken();
    
      var wxInfo = WXService.getWeChat();
      var l = this;
    
      l.showContactDetail = false;
      l.showLoading = true;
      l.deviceList = [];
      l.status = {
        onCount: 0,
        offCount: 0,
        heatCount: 0
      };
    
      if (!l.deviceList.length) {
        l.showLoading = false;
      }
    
      DeviceService.getDeviceList(wxInfo.openId).then(function(result) {
        if (result) {
          result.devices.map(function(d) {
            d.userId = result.UserId;
          })
          l.deviceList = result.devices;
          l.deviceList.map(function(device, idx) {
            getDeviceDetail(device, idx);
          })
        } else {
          l.deviceList = [];
          l.showLoading = false;
        }
    
        $interval(function() {
          refresh();
        }, 10, 1);
      }, function(error) {
        l.deviceList = [];
        l.showLoading = false;
      });
    
      function getDeviceDetail(device, idx) {
        DeviceService.setDevice(wxInfo.openId, '000000000f010300030000000000', device.physicalDeviceId, device.deviceId, '65').then(function(result) {
          var deStr = DeviceService.base64decode(result);
          var hexStr = DeviceService.CharToHex(deStr);
          var arr = hexStr.split(',');
          var reg = /^[0-9]+.?[0-9]*$/;
    
          if (arr[1].indexOf('f') != -1 && (arr[3] == '1' || arr[3] == '3')) {
            
            if (arr[7].length == 1) {
              arr[7] = '0' + arr[7];
            }
            device.temperature = DeviceService._16210(arr[6] + arr[7]);
            device.expectedTemp = parseInt(arr[8], 16);
            device.isOn = true;
            device.editable = arr[3] == "1";
            device.heatOn = arr[4];
          } else {
            device.temperature = '0';
            device.isOn = false;
            device.heatOn = 0;
          }
    
          if (!reg.test(device.temperature)) {
            device.temperature = '0';
          }
    
          if (idx == l.deviceList.length - 1) {
            l.showLoading = false;
            getStatus();
          }
        }, function() {
          if (idx == l.deviceList.length - 1) {
            l.showLoading = false;
          }
        });
      }
    
      function getStatus() {
        resetStatus();
        DeviceService.setDevicesInfo(l.deviceList);
        l.deviceList.map(function(d) {
          d.isOn ? l.status.onCount++ : l.status.offCount++; +
          d.heatOn ? l.status.heatCount++ : '';
        });
      }
    
      function refresh() {
        l.timer = $interval(function() {
          if (!l.deviceList.length) {
            $interval.cancel(l.timer);
            return;
          }
          l.deviceList.map(function(device, idx) {
            getDeviceDetail(device, idx);
          })
        }, 1000 * 5);
      }
    
      function resetStatus() {
        l.status = {
          onCount: 0,
          offCount: 0,
          heatCount: 0
        };
      }
    
      l.contactUs = function() {
        l.showContactDetail = !l.showContactDetail;
      }
    
      l.detail = function(device, idx) {
        if (!device.temperature) {
          alert('设备数据初始化中，请稍后');
          return;
        }
        WXService.setListToDetail(true);
        DeviceService.setSelected(idx);
        $state.go('DeviceDetail', {
          idStr: device.deviceId + '-' + device.physicalDeviceId
        });
      }
    
      l.options = function(path) {
        if (path == 'ScanDeviceId') {
          WXService.setListToScan(true);
        }
        $interval.cancel(l.timer);
        $state.go(path);
      }
    });
    